/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.persistencesubsystem;

import assisisw.smartcal.entitysubsystem.Calendar;
import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.utilities.Configuration;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author Francesco
 */
@Stateless ()
public class PostgreSQLJPAEventDAO implements IEventDAO 
{

    // Static Fields
    
    /***/
    private static final String FIND_ALL_QUERY_STRING = "SELECT e FROM Event AS e" ;
    
    /***/
    private static final String FIND_BY_CALENDAR_QUERY_STRING = "SELECT e FROM Event AS e WHERE e.owningCalendar = :owningCalendar" ;
    
    /***/
    private static final String REMOVE_BY_ID_QUERY_STRING = "DELETE FROM Event as e WHERE e.id = :id" ;
    
    // Instance Fields
    
    /***/
    private TypedQuery < Event > findAllQuery ;
    
    /***/
    private TypedQuery < Event > findByCalendarQuery ;
    
    /***/
    private Query deleteByIdQuery ;
    
    /***/
    @PersistenceContext ( unitName = Configuration.PU_NAME , type = PersistenceContextType.TRANSACTION )
    private EntityManager entityManager ;
    
    // Instance Methods

    /***/
    public PostgreSQLJPAEventDAO() {}
    
    /***/
    @PostConstruct 
    public void init () 
    {
        findAllQuery = entityManager.createQuery ( FIND_ALL_QUERY_STRING , Event.class ) ;
        findByCalendarQuery = entityManager.createQuery ( FIND_BY_CALENDAR_QUERY_STRING, Event.class ) ;
        deleteByIdQuery = entityManager.createQuery ( REMOVE_BY_ID_QUERY_STRING ) ;
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public Iterable < Event > findAll () 
    {
        Iterable < Event > res ;
        res = findAllQuery.getResultList () ;
        return res ;
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public Iterable < Event > findByCalendar ( Calendar calendar ) 
    {
        Iterable < Event > res ;
        if ( calendar != null )
        {
            findByCalendarQuery.setParameter ( "owningCalendar", calendar ) ;
            res = findByCalendarQuery.getResultList () ;
        }
        else
            throw new IllegalArgumentException () ;
        return res ;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public Event findById ( int id ) 
    {
        Event res ;
        res = entityManager.find ( Event.class , id ) ;
        return res ;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void insert ( Event event ) 
    {
        if ( event != null )
        {
            entityManager.persist ( event ) ;
            entityManager.flush () ;
        }
        else
            throw new IllegalArgumentException () ;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void update ( Event event ) 
    {
        if ( event != null )
        {           
            entityManager.merge ( event ) ;
            entityManager.flush();
        }
        else
            throw new IllegalArgumentException () ;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void delete ( Event event ) 
    {
        Event e ;
        int res ;
        if ( event != null )
        {
            e = entityManager.merge(event);
            entityManager.remove(e);
            entityManager.flush () ;
        }
        else
            throw new IllegalArgumentException () ;
    }

    
    
}
